<?php

namespace app\admin\controller\insurance;

use app\admin\model\InsuranceFactory;
use app\admin\service\TriggerService;
use app\common\constants\MenuConstant;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnnotation;
use app\common\controller\AdminController;
use app\Request;
use think\App;
use think\response\Json;

#[ControllerAnnotation(title: '合作厂')]
class Factory extends AdminController
{
    protected array $sort = [
        'id'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        self::$model = InsuranceFactory::class;
    }

    #[NodeAnnotation(title: '列表', auth: true)]
    public function index(Request $request): Json|string
    {
        if ($request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParams();
            $count = self::$model::where($where)->where('status', "<", 2)->count() ?: 0;
            $list  = self::$model::where($where)->where('status', "<", 2)->order($this->sort)->page($page, $limit)->select()->toArray() ?: [];
            $data  = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    #[NodeAnnotation(title: '添加', auth: true)]
    public function add(Request $request): string
    {
        if ($request->isPost()) {
            $post = $request->post();
            try {
                $save = self::$model::create($post);
            }catch (\Exception $e) {
                $this->error('保存失败');
            }
            if ($save) {
                // TriggerService::updateMenu();
                $this->success('保存成功');
            }else {
                $this->error('保存失败');
            }
        }

        return $this->fetch();
    }
    #[NodeAnnotation(title: '编辑', auth: true)]
    public function edit(Request $request, $id = 0): string
    {
        $row = self::$model::find($id);
        empty($row) && $this->error('数据不存在');
        if ($request->isPost()) {
            $post = $request->post();
            try {
                $post['update_time'] = time();
                $save = $row->save($post);
            }catch (\Exception $e) {
                $this->error('保存失败');
            }
            if ($save) {
                // TriggerService::updateMenu();
                $this->success('保存成功');
            }else {
                $this->error('保存失败');
            }
        }
        $this->assign([
            'id'          => $id,
            'row'         => $row,
        ]);
        return $this->fetch();
    }
    #[NodeAnnotation(title: '删除', auth: true)]
    public function delete(Request $request): void
    {
        $this->checkPostRequest();
        $id  = $request->param('id');
        $row = self::$model::whereIn('id', $id)->select();
        empty($row) && $this->error('数据不存在');
        try {
            $save = $row->update(['status' => 2, 'delete_time' => time()]);
        }catch (\Exception $e) {
            $this->error('删除失败');
        }
        if ($save) {
            // TriggerService::updateMenu();
            $this->success('删除成功');
        }else {
            $this->error('删除失败');
        }
    }
}
